/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xume.raob.core;

import static org.xume.raob.util.ReflectionUtils.createInstance;
import static org.xume.raob.util.ReflectionUtils.isBuildMethod;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.xume.raob.ObjectInstantiationStrategy;
import org.xume.raob.annotations.BuildStepFactory;
import org.xume.raob.exceptions.InstatiationException;
import org.xume.raob.util.ArrayUtils;

/**
 * @author Johan Siebens
 */
public final class ObjectBuilderInvocationHandler<T> implements InvocationHandler {

	private IntrospectionResult introspectionResult;

	private ObjectInstantiationStrategy<T> instantiationStrategy;

	private final List<BuildStep> buildSteps;

	public ObjectBuilderInvocationHandler(ObjectInstantiationStrategy<T> instantiationStrategy, IntrospectionResult introspectionResult) {
		this.instantiationStrategy = instantiationStrategy;
		this.introspectionResult = introspectionResult;
		this.buildSteps = new LinkedList<BuildStep>();
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

		if (isBuildMethod((method))) {
			return buildObject();
		}

		if (isBuildStep(method)) {
			BuildStepFactory<?> buildStepFactory = introspectionResult.getBuildStepFactory(method);
			this.buildSteps.addAll(Arrays.asList(buildStepFactory.getBuildSteps(args)));
			return proxy;
		}

		if (isPostBuildMethod(method)) {
			Object[] actualArgs = ArrayUtils.addFirst(args, buildObject());
			PostBuildMethod postBuildMethod = introspectionResult.getPostBuildMethod(method);
			return postBuildMethod.execute(actualArgs);
		}

		throw new UnsupportedOperationException("Don't know how to execute method [" + method + "]");

	}

	private Object buildObject() {
		Object result = null;
		if (instantiationStrategy != null) {
			result = instantiationStrategy.createNewObject();
		} else {
			result = new DefaultInstantiationStrategy().createNewObject();
		}
		for (BuildStep buildStep : buildSteps) {
			buildStep.execute(result);
		}
		return result;
	}

	private boolean isPostBuildMethod(Method method) {
		return introspectionResult.isPostBuildMethod(method);
	}

	private boolean isBuildStep(Method method) {
		return introspectionResult.isBuildStep(method);
	}

	@SuppressWarnings("unchecked")
	private class DefaultInstantiationStrategy implements ObjectInstantiationStrategy {

		public Object createNewObject() throws InstatiationException {
			Class<?> targetClass = introspectionResult.getTargetClass();
			return createInstance(targetClass);
		}

	}

}
